package com.desuntech.business
{
	import C4D.kbmMWConnection;
	import C4D.kbmMWEvent;
	import C4D.kbmMWQuery;
	import C4D.kbmMWResponse;
	import C4D.kbmMWRow;
	
	import com.desuntech.utils.MenuHelper;
	
	import flash.events.Event;
	
	import mx.controls.Alert;

	public class SysauthBusiness
	{
		private var _conn:kbmMWConnection;
		private var _resultHandler:Function;
		private var _q:kbmMWQuery;
		private var _formsForRole:Array;
		
		public function SysauthBusiness(conn:kbmMWConnection)
		{
			_conn = conn;
		}
		
		
		// get roles
		public function getMenuXmlAuthorized(personid:int, resultHandler:Function):void {
			_resultHandler = resultHandler;
			_q = new kbmMWQuery();
			_q.Connection = _conn;
			_q.addEventListener(kbmMWEvent.QueryDataAvailableEvent, onRolesAvailable);
			_q.addEventListener(kbmMWEvent.FailureEvent, onRolesFault);
			_q.Query = 'SELECT * FROM ' + AppConstants.SYSAUTH_EMPLOYEEROLES + ' WHERE personid = ' + personid;
			_q.Open();
		}
		
		private function onRolesAvailable(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, onRolesAvailable);
			q.removeEventListener(kbmMWEvent.FailureEvent, onRolesFault);
			
			var result:Array = (event.target as kbmMWQuery).Rows.slice();
			if(result.length) {
				getFormsForRole(result[0]);
			} else {
				_resultHandler(new XML('<menu label="未授权"/>'));
			}
		}
		
		private function onRolesFault(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, onRolesAvailable);
			q.removeEventListener(kbmMWEvent.FailureEvent, onRolesFault);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n");
		}
		
		// get formids
		private function getFormsForRole(role:kbmMWRow):void {
			_q = new kbmMWQuery();
			_q.Connection = _conn;
			_q.addEventListener(kbmMWEvent.QueryDataAvailableEvent, onFormsAvailable);
			_q.addEventListener(kbmMWEvent.FailureEvent, onFormsFault);
			_q.Query = 'SELECT * FROM sysauth_sysroleforms INNER JOIN sysauth_sysforms ON sysauth_sysroleforms.formid = sysauth_sysforms.formid WHERE sysauth_sysroleforms.roleid = ' + role['roleid'];
			_q.Open();
		}
		
		private function onFormsAvailable(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, onFormsAvailable);
			q.removeEventListener(kbmMWEvent.FailureEvent, onFormsFault);
			
			var result:Array = (event.target as kbmMWQuery).Rows.slice();
			if(result.length) {
				_formsForRole = result;
				getForms();
			} else {
				_resultHandler(new XML('<menu label="未授权"/>'));
			}
		}
		
		private function onFormsFault(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, onFormsAvailable);
			q.removeEventListener(kbmMWEvent.FailureEvent, onFormsFault);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n"); 
		}
		
		// get all forms
		private function getForms():void {
			_q = new kbmMWQuery();
			_q.Connection = _conn;
			_q.addEventListener(kbmMWEvent.QueryDataAvailableEvent, onAllFormsAvailable);
			_q.addEventListener(kbmMWEvent.FailureEvent, onAllFormsFault);
			_q.Query = 'SELECT * FROM sysauth_sysforms';
			_q.Open();
		}
		
		private function onAllFormsAvailable(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, onAllFormsAvailable);
			q.removeEventListener(kbmMWEvent.FailureEvent, onAllFormsFault);
			
			var result:Array = (event.target as kbmMWQuery).Rows.slice();
			if(result.length) {
				var menuAll:XML = MenuHelper.buildMenuAll(result);
				var menuForRole:XML = MenuHelper.buildMenuForRole(menuAll, _formsForRole);
				_resultHandler(menuForRole);
			}
		}
		
		private function onAllFormsFault(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, onAllFormsAvailable);
			q.removeEventListener(kbmMWEvent.FailureEvent, onAllFormsFault);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n"); 
		}
	}
}